/**
 * 后台JS主入口
 */
// var layer = layui.layer,
//     element = layui.element(),
//     laydate = layui.laydate,
//     form = layui.form();
/**
 * AJAX全局设置
 */
$.ajaxSetup({
    type: "post",
    dataType: "json"
});

//导航栏依赖
layui.use('element', function(){
    var element = layui.element;
});

/**
 * 通用单图上传
 */
layui.use('upload',function () {
    var upload = layui.upload;

    upload.render({
        elem:'#one-image-upload',
        url: "/index.php/api/upload/upload",
        type: 'image',
        ext: 'jpg|png|gif|bmp',
        before:function (obj) {
            layer.load();
        },
        done: function (data) {
            if (data.error === 0) {
                document.getElementById('thumb').value = data.url;
                document.getElementById('show-img').src = data.url;
                $('#layui-img').show();
                layer.closeAll('loading');
                layer.msg("上传成功");
            } else {
                layer.msg(data.message);
            }
        }
    });
});


/**
 * 图集
 */

$('#file').fileupload({
    url: "/index.php/api/upload/upload",
    dataType: 'json',
    progressall: function (e, data) {
        layer.load();
    },
    done: function (e, data) {
        if (data.result.error === 0) {
            $('#demo2').append('<div class="layui-inline"><img src="'+ data.result.url +'" alt="" class="layui-upload-img" style="width: 60px;height: 60px;"> <input type="hidden" name="photos[]" value="'+data.result.url+'"><div><button  onclick="$(this).parent(\'div\').parent(\'div\').remove();">删除</button></div></div>');
            layer.closeAll('loading');
            layer.msg("上传成功");
        } else {
            layer.msg(data.message);
        }
    }
});

/**
 * 通用日期时间选择
 */
$('.datetime').on('click', function () {
    layui.use('laydate',function () {
        var laydate = layui.laydate;
        laydate.render({
            elem:this,
            istime: true,
            format: 'YYYY-MM-DD hh:mm:ss'
        });
    });
});

/**
 * 通用表单提交(AJAX方式)
 */
layui.use('form',function(){
    var form = layui.form;

    form.on('submit(*)', function (data) {
        $.ajax({
            url: data.form.action,
            type: data.form.method,
            data: $(data.form).serialize(),
            beforeSend: function(){
                layer.msg('正在提交');
            },
            success: function (info) {
                if (info.code === 1) {
                    setTimeout(function () {
                        location.href = info.url;
                    }, 1000);
                }
                layer.msg(info.msg);
            }
        });

        return false;
    });

});

/**
 * 通用批量处理（审核、取消审核、删除）
 */
$('.ajax-action').on('click', function () {
    var _action = $(this).data('action');
    layer.open({
        shade: false,
        content: '确定执行此操作？',
        btn: ['确定', '取消'],
        yes: function (index) {
            $.ajax({
                url: _action,
                data: $('.ajax-form').serialize(),
                success: function (info) {
                    console.log(info);
                    if (info.code === 1) {
                        setTimeout(function () {
                            location.href = info.url;
                        }, 1000);
                    }
                    layer.msg(info.msg);
                }
            });
            layer.close(index);
        }
    });

    return false;
});

/**
 * 通用全选
 */
$('.check-all').on('click', function () {
    $(this).parents('table').find('input[type="checkbox"]').prop('checked', $(this).prop('checked'));
});

/**
 * 通用删除
 */
$('.ajax-delete').on('click', function () {
    var _href = $(this).attr('href');
    layer.open({
        shade: false,
        content: '确定删除？',
        btn: ['确定', '取消'],
        yes: function (index) {
            $.ajax({
                url: _href,
                type: "get",
                success: function (info) {
                    if (info.code === 1) {
                        setTimeout(function () {
                            location.href = info.url;
                        }, 1000);
                    }
                    layer.msg(info.msg);
                }
            });
            layer.close(index);
        }
    });

    return false;
});

/**
 * 清除缓存
 */
$('#clear-cache').on('click', function () {
    var _url = $(this).data('url');
    if (_url !== 'undefined') {
        $.ajax({
            url: _url,
            success: function (data) {
                if (data.code === 1) {
                    setTimeout(function () {
                        location.href = location.pathname;
                    }, 1000);
                }
                layer.msg(data.msg);
            }
        });
    }

    return false;
});

/**
 * 同步数据
 */
$('#same-data').on('click', function () {
    var _url = $(this).data('url');
    if (_url !== 'undefined') {
        $.ajax({
            url: _url,
            success: function (data) {
                if (data.code === 1) {
                    setTimeout(function () {
                        location.href = location.pathname;
                    }, 1000);
                }
                layer.msg(data.msg);
            }
        });
    }

    return false;
});